Page({
  data: {
    // 茶类
    items: [
      { name: '绿茶', id: "2", checked: 'checked' },
      { name: '红茶', id: "1" },
      { name: '白茶', id: "3" },
      { name: '普洱茶', id: "4" },
      { name: '乌龙茶', id: "5" },
      { name: '黑茶', id: "6" },
    ],
    // 宽度设置
    winW:"",
    // 销售额选择
    casIndex: 0,
    casArray: ['100万元以下', '100~300万元', '300~1000万元', '1000~3000万元', '3000~10000万元', '1~3亿元', '3亿元以上'],
    // 产区
    chanqu: [],
    bangming: [
      // 榜名
      { id: "1", name: "匠人", checked: 'checked' },
      { id: "2", name: '品牌' },
      { id: "3", name: '珍品' },
    ],
    ry: [
      { id: "1", name: '正山出品', checked: '' }, { id: "2", name: '古树老丛', checked: '' }, { id: "3", name: '传统工艺', checked: '' }, { id: "4", name: '大师手制', checked: '' },
    ],
    wechat: [
      { id: "1", name: "微信", checked: 'checked' },
      { id: "2", name: '手机' },
    ],
    manandgril: [
      { id: "1", name: "男士", checked: 'checked' },
      { id: "2", name: '女士' },
    ],
    exist_id:1,
    rr: [],
    sex: "1",
    contact: "1",
    dats_ry: [],
    nametiao: "none",
    xiantiao: true,
    bd_id: 1,
    pp: 'none',
    jr: 'block',
    zp: "none",

    // 修改内容显示
    name: '', //词条名字
    phone: '', //手机
    wechats: '', //微信
    contact: '', //联系人
    detailintro: '',//详细信息
    relatedwords: '', //关键词
    sale: '', //销售额
    company: '', //公司
    pnumber: '', //公众号
    producer: '', //出品人
    orther: '', //其他荣誉
    price: '', //零售价
    briefintro:'',//简介
  },
  //男女选择
  manandgrilchoose: function (e) {
    var id = e.detail.value;
    var pos = this.data.manandgril;
    pos = this.ss(pos, id);
    this.setData({
      sex: id,
      manandgril: pos,
    })
  },
  //联系方式选择
  wechatchoose: function (e) {
    var id = e.detail.value;
    var pos = this.data.wechat;
    pos = this.ss(pos, id);
    this.setData({
      contact: id,
      wechat: pos,
    })
  },
  // 判断词条是否存在
  iscture: function (e) {
    var name = e.detail.value;
    var that = this;
    if (!name) {
      that.setData({
        nametiao: "none",
      })
      return;
    }
    wx.request({
      url: getApp().AppUrl + 'index.php/api/api/search_ct',
      method: 'GET',
      data: {
        name: "" + name,
      },
      success: function (res) {
        if (res.data == 1) {
          var issts = true;
        } else {
          var issts = false;
        }
        that.setData({
          nametiao: 'block',
          xiantiao: issts,
        })
      }
    })
  },
  // 茶类
  radioChange: function (e) {
    var that = this;
    var id = e.detail.value;
    var pos = that.data.items;
    pos = that.ss(pos, id);
    that.setData({
      items: pos,
    })
    wx.request({
      // 选择请求茶类
      url: getApp().AppUrl + 'index.php/api/api/getTeaArea',
      method: 'GET',
      data: {
        id: "" + id
      },
      success: function (res) {
        console.log(res.data)
        var chanqu = res.data;
        if (chanqu) {
          for (var index in chanqu) {
            chanqu[0].checked = 'checked';
          }
        }
        that.setData({
          chanqu: chanqu
        })
      }
    })

  },
  //产区选择
  chaquchoose: function (e) {
    var id = e.detail.value;
    var pos = this.data.chanqu;
    for (var index in pos) {
      if (pos[index].teaarea_id == id) {
        pos[index].checked = 'checked';
      } else {
        pos[index].checked = '';
      }
    }
    this.setData({
      chanqu: pos,
    })
  },
  // 荣誉多选
  rychoose: function (e) {
    var that = this;
    var ry = that.data.ry;
    var ids = parseInt(e.target.dataset.id);
    var rr = that.data.rr;
    for (var index in ry) {
      if (ry[index].id == ids) {
        if (ry[index].checked == '') {
          ry[index].checked = 'checked';
        } else {
          ry[index].checked = ''
        }
      }
    }
    if (rr.length > 0) {
      var index = rr.indexOf(ids);
      if (index > -1) {
        rr.splice(index, 1);
      } else {
        rr.push(ids);
      }
    } else {
      rr.push(ids);
    }
    that.setData({
      ry: ry,
      rr: rr
    })
    // console.log('发生change事件，携带value值为：', ids)
  },
  // 榜单选择
  bangmingchoose: function (e) {
    this.setData({
      bd_id: e.detail.value,
    })
    var id = this.data.bd_id;
    var pos = this.data.bangming;
    var ry = this.data.ry;
    var data = this.bdchoose(ry, id);
    pos = this.ss(pos, id);
    this.setData({
      bangming: pos,
      jr: data['jr'] ,
      pp: data['pp'],
      zp: data['zp'],
      rr: [],
    })
  },
  locationlw: function () {
    wx.navigateTo({
      url: '../Contact/Contact'
    })
  },
  locationdesc: function () {
    wx.navigateTo({
      url: '../Description/Description'
    })
  },
  // 共用函数选择id
  ss: function (pos, id) {
    for (var index in pos) {
      if (pos[index].id == id) {
        pos[index].checked = 'checked';
      } else {
        pos[index].checked = '';
      }
    }
    return pos;
  },
  // 茶类公共函数
  cl: function (pos, id){
    for (var index in pos) {
      if (pos[index].teaarea_id == id) {
        pos[index].checked = 'checked';
      } else {
        pos[index].checked = '';
      }
    }
    return pos;
  },
  // 公共榜单选择
  bdchoose: function (ry, id){
    if (id == 2) {
      var jr = 'none';
      var pp = 'block';
      var zp = 'none';
      for (var index in ry) {
        ry[index].checked = '';
      }
    } else if (id == 3) {
      var jr = 'none';
      var pp = 'none';
      var zp = 'block';
    } else {
      var jr = 'block';
      var pp = 'none';
      var zp = 'none';
      for (var index in ry) {
        ry[index].checked = '';
      }
    }
    this.setData({
      ry:ry
    })
    var data = new Array();
    data['jr'] = jr;
    data['pp'] = pp;
    data['zp'] = zp;
    return data;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var o = options;
    wx.showLoading({
      title: '加载中',
      mask: true,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
    wx.getSystemInfo({
      success: function (res) {
        console.log(res);
        var winW = res.windowWidth * 80 / 100;
        console.log(winW)
        that.setData({
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth,
          winW: winW,
        })
      }
    });
    // 请求茶类
    wx.request({
      url: getApp().AppUrl + '/index.php/api/api/getTeaCase',
      data: {},
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var dats = res.data;
        dats = that.ss(dats, o.tid);
        that.setData({
          items: dats,
        })
      }
    })
    that.getteacase(o.tid,o.hid);
    that.loadingget_ct(o.id, o.sid);
    wx.hideLoading();
  },
  // 表单提交
  formSubmit: function (e) {
    var datas = e.detail.value;
    var contact = this.data.contact;
    var rr = this.data.rr;
    var sex = this.data.sex;
    var xiantiao = this.data.xiantiao;
    datas["sex"] = sex;
    datas['exist_id'] = this.data.exist_id;
    if (!datas['bangdan_id'] || !datas['teacase_id'] || !datas['teaarea_id'] || !datas['name']) {
      wx.showToast({
        title: '词条内容不能为空',
        icon: 'fail',
        duration: 2000
      })
      return;
    }
    if (!xiantiao) {
      wx.showToast({
        title: '词条内容已存在',
        icon: 'fail',
        duration: 2000
      })
      return;
    }
    if (datas['bangdan_id'] == 3) {
      for (var s = 0; s < rr.length; s++) {
        if (rr[s] == "1") {
          datas['zs'] = "√";
        }
        if (rr[s] == "2") {
          datas['gs'] = "√";
        } 
        if (rr[s] == "3") {
          datas['ct'] = "√";
        } 
        if (rr[s] == "4") {
          datas['ds'] = "√";
        } 
      }
    }
    if (contact == 1) {
      datas['wechat'] = datas['lxfs'];
    } else {
      datas['phone'] = datas['lxfs'];
    }
    var that = this;
    // 请求茶类
    wx.request({
      url: getApp().AppUrl + '/index.php/api/set/updata_ct_name',
      data: datas,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data == 1) {
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 1000
          })
        } else {
          wx.showToast({
            title: '提交失败',
            icon: 'success',
            duration: 2000
          })
        }
        setTimeout(function () {
          wx.navigateBack({})
        }, 2000)
      },
    })
    console.log(datas)
    console.log('联系人' + this.data.contact)
    console.log('男女' + this.data.sex)
  },
  bindCasPickerChange: function (e) {
    console.log('销售额选的是', this.data.casArray[e.detail.value])
    if (e.detail.value == 4) {
      this.setData({ reply: true })
    } else {
      this.setData({ reply: false })
    }
    console.log(e.detail.value)
    this.setData({
      casIndex: e.detail.value
    })

  },
  // 获取修改词条的信息
  loadingget_ct: function (id,sid){
    var that = this;
    var pos = that.data.bangming;
    var ry = that.data.ry;
    var data = that.bdchoose(ry, id);
    console.log(sid)
    pos = this.ss(pos, id);
    wx.request({
      url: getApp().AppUrl + 'index.php/api/api/getDetail',
      method: 'GET',
      data: {
        id: "" + sid,
        bangdan_id: "" + id,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res);
        var company = '';
        var briefintro = "";
        var order = '';
        var price = '';
        var relatedwords = '';
        var pnumber = '';
        var res = res.data;
        if (res.zs == "√"){

        }
        if (res.gs == "√") {

        }
        if (res.vt == "√") {

        }
        if (res.ds == "√") {

        }
        if (!res.briefintro){
          briefintro = ""
        }else{
          briefintro = res.briefintro;
        }
        if(res.order){
          order = res.order;
        }
        if (res.price){
          price = res.price;
        }
        if (res.company){
          company = res.company;
        }
        if (res.relatedwords){
          relatedwords = res.relatedwords
        }
        if (res.pnumber){
          pnumber = res.pnumber
        }
        that.setData({
          exist_id: sid,
          price: price,
          relatedwords: relatedwords,
          order: order,
          pnumber: pnumber,
          company: company,
          briefintro: briefintro,
          producer: res.producer,
          name: res.name,
          detailintro: res.detailintro,
          bangming: pos,
          jr: data['jr'],
          pp: data['pp'],
          zp: data['zp'],
          rr: [],
        })
       
      
      }
    })
  },
  // 请求茶类信息
  getteacase:function(id,hid){
    var that = this;
    wx.request({
      // 请求茶类
      url: getApp().AppUrl + 'index.php/api/api/getTeaArea',
      method: 'GET',
      data: {
        id: id
      },
      success: function (res) {
        var chanqu = res.data;
        chanqu = that.cl(chanqu, hid);
        that.setData({
          chanqu: chanqu
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})